#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import traceback

from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.FileItem import FileItem
from alipay.aop.api.domain.AlipayTradeAppPayModel import AlipayTradeAppPayModel
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
from alipay.aop.api.domain.AlipayTradePayModel import AlipayTradePayModel
from alipay.aop.api.domain.GoodsDetail import GoodsDetail
from alipay.aop.api.domain.SettleDetailInfo import SettleDetailInfo
from alipay.aop.api.domain.SettleInfo import SettleInfo
from alipay.aop.api.domain.SubMerchant import SubMerchant
from alipay.aop.api.request.AlipayOfflineMaterialImageUploadRequest import AlipayOfflineMaterialImageUploadRequest
from alipay.aop.api.request.AlipayTradeAppPayRequest import AlipayTradeAppPayRequest
from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest
from alipay.aop.api.request.AlipayTradePayRequest import AlipayTradePayRequest
from alipay.aop.api.response.AlipayOfflineMaterialImageUploadResponse import AlipayOfflineMaterialImageUploadResponse
from alipay.aop.api.response.AlipayTradePayResponse import AlipayTradePayResponse
from alipay.aop.api.util.SignatureUtils import verify_with_rsa
from django.conf import settings
import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    filemode='a',)
logger = logging.getLogger(__name__)

class Alipay(object):
    def __init__(self):
        alipay_client_config = AlipayClientConfig()
        alipay_client_config.server_url = settings.ALIPAY_SERVER_URL
        alipay_client_config.app_id = settings.ALIPAY_APP_ID
        alipay_client_config.app_private_key = settings.ALIPAY_APP_PRIVATE_KEY
        alipay_client_config.alipay_public_key = settings.ALIPAY_PUBLIC_KEY
        self.client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger)

    def trade_page(self,out_trade_no,total_amount,subject,body,product_code,passback_params,data_id):
        # 页面接口示例：alipay.trade.page.pay
        # 对照接口文档，构造请求对象
        #这5个项目是必填的
        model = AlipayTradePagePayModel()
        model.out_trade_no = out_trade_no    #商户订单号,必填
        model.total_amount = total_amount       #订单总金额，必填
        model.subject = subject         # 订单标题，必填
        model.body = body
        model.product_code = product_code          #销售产品码，与支付宝签约的产品码名称，必填
        model.passback_params=passback_params
        request = AlipayTradePagePayRequest(biz_model=model)
        request.return_url = settings.ALIPAY_RETURN_URL
        request.notify_url = settings.ALIPAY_NOTIFY_URL
        # 得到构造的请求，如果http_method是GET，则是一个带完成请求参数的url，如果http_method是POST，则是一段HTML表单片段
        response = self.client.page_execute(request, http_method="GET")
        return response

    def verify_sign(self,unsigned_string,sign):
        return verify_with_rsa(settings.ALIPAY_PUBLIC_KEY,bytes(unsigned_string,encoding='utf-8'),sign)